<?php

class CTable
{
  /** 
   * Standard Table Properties
   * 
   * 
   * */
  
  private $db_conn;          // database connection
  protected $name;           // table name
  protected $rows_per_page;  // used in pagination
  protected $pageno;         // current page number
  protected $lastpage;       // highest page number
  protected $fieldlist = array();      // list of fields in this table
  protected $data = array();     // data from the database
  protected $errors;         // array of error messages


public function __construct($db_connection)// constructor
{ 
  $this->db_conn = $db_connection;
  $this->numrows = 0;
  $this->lastpage = 0;
  $this->pageno=0;
  
  //$this->name = $name;
  //$this->rows_per_page = $rows_per_page;
  //$this->fieldlist = $table_field_array;
  //$this->pageno = $page_number;
} 

/**
* set CTable property
* @property name
* @value of property
* */

public function __set($property,$value)
{ $this->$property = $value; } 

public function __get($property)
{
  $vars = array("lastpage","pageno");
 if (in_array($property_name, $vars))
 {return $this->$property;} 
 else { return 0;}

}
///////////////////////////////////////////////////////////
public function getData ($where="")
{
 $pageno = $this->pageno;
 $rows_per_page = $this->rows_per_page;
 
// $dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);
if (empty($where)) 
 {$where_str = NULL; } 
else
 {$where_str = "WHERE $where";} // if

$query = "SELECT count(*) FROM $this->name $where_str";
$result = mysqli_query($this->db_conn,$query) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysqli_fetch_row($result);
$this->numrows = $query_data[0]; /// 

if ($this->numrows <= 0) // no data table
{
 $this->pageno = 0;
 return;
} // if

/////////////////////////////////// get page number & set last page number
if ($rows_per_page > 0)
{
 $this->lastpage = ceil($this->numrows/$rows_per_page); // set page number 
}
else 
{
 $this->lastpage = 1;
} // if
//////////////////////////////// to set current page number 
if ($pageno == '' OR $pageno <= '1') 
{
  $pageno = 1;
} 
elseif ($pageno > $this->lastpage)
{
 $pageno = $this->lastpage;
} // if
$this->pageno = $pageno;

 if ($rows_per_page > 0) 
 {
  $limit_str = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
 } 
  else 
 {
  $limit_str = NULL;
 } // if

$query = "SELECT * FROM $this->name $where_str $limit_str";
$result = mysqli_query($this->db_conn,$query) or trigger_error("SQL", E_USER_ERROR);

while ($row = mysqli_fetch_assoc($result)) 
{
 $this->data[] = $row;
} // while
// mysql_free_result($result);
return $this->data;
} // getData


/** 
* $fieldarray === association array with field name keys
* 
* 
* */

public function insertRecord ($fieldarray)
{
///$dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);
$fieldlist = $this->fieldlist;
$fields='';
$values='';

foreach ($fieldarray as $field => $fieldvalue)
{
 if (!in_array($field, $fieldlist)) 
 {
  unset ($fieldarray[$field]);
 } // if
 else
 {
   //$values.=',?';
   $fields.=$field;
   $values.='"'.$fieldvalue.'",';
 } 
} // foreach

////////////////// prepare query ////////////////////
$fields = rtrim($fields,',');
$values= rtrim($values,',');

$query= "INSERT INTO $this->name ($fields) VALUES($values)";
$result = @mysqli_query( $this->db_conn,$query);

if (mysqli_errno($this->db_conn) <> 0) 
{
  if (mysqli_errno($this->db_conn) == 1062) 
 {
  $this->errors[] = "A record already exists with this ID.";
 } 
 else 
 {
  echo mysqli_error($this->db_conn);
 } // if

} // if

return;



} // insertRecord
////////////////////////////////////////////////////////////////////////////////////////////////////

public function updateRecord ($fieldarray)

{

$this->errors = array();



global $dbconnect, $query;

$dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);



$fieldlist = $this->fieldlist;

foreach ($fieldarray as $field => $fieldvalue) {

if (!in_array($field, $fieldlist)) 
{
 unset ($fieldarray[$field]);
} // if

} // foreach



$where = NULL;

$update = NULL;

foreach ($fieldarray as $item => $value) {

if (isset($fieldlist[$item]['pkey'])) {

$where .= "$item='$value' AND ";

} else {

$update .= "$item='$value', ";

} // if

} // foreach



$where = rtrim($where, ' AND ');

$update = rtrim($update, ', ');



$query = "UPDATE $this->name SET $update WHERE $where";

$result = mysql_query($query, $dbconnect) or trigger_error("SQL", E_USER_ERROR);



return;



} // updateRecord



//////////////////////////////////////////////////////////////////////////////

public function deleteRecord ($fieldarray)

{

$this->errors = array();
 $query;

global $dbconnect = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);



$fieldlist = $this->fieldlist;

$where = NULL;
foreach ($fieldarray as $item => $value)
 {

if (isset($fieldlist[$item]['pkey'])) {

$where .= "$item='$value' AND ";

} // if

} // foreach

$where = rtrim($where, ' AND ');



$query = "DELETE FROM $this->name WHERE $where";

$result = mysql_query($query, $dbconnect) or trigger_error("SQL", E_USER_ERROR);



return;



} // deleteRecord









} // end class





?>